package com.lele.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lele.pojo.Book;

import com.lele.util.*;


@WebServlet("/updateBook")
public class updateBook extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String bookid=request.getParameter("bookid");
        String title=request.getParameter("title");
        String author=request.getParameter("author");
        String publisher=request.getParameter("publisher");
        String price=request.getParameter("price");
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "update books set title=?,author=?,publisher=?,price=? where bookid=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,title);
            pstmt.setString(2,author);
            pstmt.setString(3,publisher);
            pstmt.setDouble(4,Double.parseDouble(price));
            pstmt.setDouble(5,Integer.parseInt(bookid));
            pstmt.executeUpdate();
            System.out.println("修改成功");
            request.getRequestDispatcher("success.jsp").forward(request, response);
        } catch (SQLException e) {
        	System.out.println("修改失败");
        	request.getRequestDispatcher("error.jsp").forward(request, response);
            e.printStackTrace();
        }finally {
            //6. 释放资源
            JDBCUtils.close(pstmt,conn);
        }
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
